package com.margo.project.append.service;

import java.lang.reflect.Field;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.margo.project.append.dao.MarColumnNameDAO;
import com.margo.project.append.model.MarColumnNameVO;
import com.margo.project.append.model.PSBaseVO;
import com.margo.project.append.utils.LcamGenerateHelper;

@Service
public class LcamModelFactory {
	@Autowired
	private MarColumnNameDAO marColumnNameDAO;
	
	@Autowired
	private LcamGenerateHelper lcamGenerateHelper;
	
	public void createVO(String tableName,String packageName){
		List<MarColumnNameVO> lstItem=marColumnNameDAO.queryMarColumnNameList(tableName);
		StringBuilder sb=new StringBuilder();
		String className=lcamGenerateHelper.initFiledName(tableName.substring(5));
		sb.append("package ").append(packageName).append(".").append(className.toLowerCase()).append(".model; \r");
		sb.append(" \r");
		sb.append("import java.util.Date;\r");
		sb.append(" \r");
		sb.append("import javax.ejb.TransactionAttribute; \r");
		sb.append("import javax.ejb.TransactionAttributeType;  \r");
		sb.append("import javax.ejb.TransactionManagement;  \r");
		sb.append("import javax.ejb.TransactionManagementType;  \r");
		sb.append("import javax.persistence.Column;  \r");
		sb.append("import javax.persistence.Entity;  \r");
		sb.append("import javax.persistence.Id;  \r");
		sb.append("import javax.persistence.Table;  \r");
		sb.append("import javax.persistence.Temporal;  \r");
		sb.append("import javax.persistence.TemporalType;  \r");
		sb.append("import javax.persistence.Transient;  \r");
		sb.append(" \r");
		sb.append("import com.comtop.lcam.project.common.model.PSBaseVO;  \r");
		sb.append(" \r");
		
		sb.append("@Entity(name = \"Sw").append(className).append("\") \r");
		sb.append("@Table(name = \"").append(tableName).append("\") \r");
		sb.append("@TransactionAttribute(TransactionAttributeType.REQUIRED) \r");
		sb.append("@TransactionManagement(TransactionManagementType.CONTAINER) \r");

		String voName="Sw"+className+"VO";
		sb.append("public class ").append(voName).append(" extends PSBaseVO{").append(" \r");
		//私有属性
		for(int i=0;i<lstItem.size();i++) {
			MarColumnNameVO marColumnNameVO=lstItem.get(i);
			String fieldName=lcamGenerateHelper.initFiledName(marColumnNameVO.getColumnName());
			if(isExtend(fieldName)) {
				continue;
			}
			sb.append(" \r");
			marColumnNameVO.setFiledName(lcamGenerateHelper.initFiledName(marColumnNameVO.getColumnName()));
			
			if(marColumnNameVO.getDataType().equals("VARCHAR2")) {
				sb.append("\t").append("//").append(i+1).append(",")
					.append(marColumnNameVO.getDataType()).append("(").append(marColumnNameVO.getDataLength()).append(") ")
					.append(marColumnNameVO.getComments()).append(" \r");
				if(i==0) {
					sb.append("\t").append("@Id").append(" \r");
				}
				sb.append("\t").append("@Column(name = ").append("\"").append(marColumnNameVO.getColumnName()).append("\"")
					.append(", length = ").append(marColumnNameVO.getDataLength())
					.append(", nullable = ").append("N".equals(marColumnNameVO.getNullable())?"false":"true").append(")\r");
				sb.append("\t").append("private ").append("String ").append(fieldName).append("; \r");
			}else if(marColumnNameVO.getDataType().equals("NUMBER")) {
				sb.append("\t").append("//").append(i+1).append(",")
					.append(marColumnNameVO.getDataType()).append("(").append(marColumnNameVO.getDataPrecision()).append(") ")
					.append(marColumnNameVO.getComments()).append(" \r");
				sb.append("\t").append("@Column(name = ").append("\"").append(marColumnNameVO.getColumnName()).append("\"")
					.append(", length = ").append(marColumnNameVO.getDataPrecision())
					.append(", nullable = ").append("N".equals(marColumnNameVO.getNullable())?"false":"true").append(")\r");
				sb.append("\t").append("private ").append("Integer ").append(fieldName).append("; \r");
			}else if(marColumnNameVO.getDataType().equals("TIMESTAMP(6)")) {
				sb.append("\t").append("//").append(i+1).append(",")
					.append(marColumnNameVO.getDataType()).append("(").append(marColumnNameVO.getDataScale()).append(") ")
					.append(marColumnNameVO.getComments()).append(" \r");
				sb.append("\t").append("@Column(name = ").append("\"").append(marColumnNameVO.getColumnName()).append("\"")
					.append(", length = ").append(marColumnNameVO.getDataScale())
					.append(", nullable = ").append("N".equals(marColumnNameVO.getNullable())?"false":"true").append(")\r");
				sb.append("\t").append("private ").append("Date ").append(fieldName).append("; \r");
			}
		}
		sb.append(" \r");
		//get set方法
		for(MarColumnNameVO marColumnNameVO:lstItem) {
			String fieldName=lcamGenerateHelper.initFiledName(marColumnNameVO.getColumnName());
			if(isExtend(fieldName)) {
				continue;
			}
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			String setter = "set" + firstLetter + fieldName.substring(1);
			if(marColumnNameVO.getDataType().equals("VARCHAR2")) {
				sb.append("\t").append("public String ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(String ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}else if(marColumnNameVO.getDataType().equals("NUMBER")) {
				sb.append("\t").append("public Integer ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(Integer ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}else if(marColumnNameVO.getDataType().equals("TIMESTAMP(6)")) {
				sb.append("\t").append("public Date ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(Date ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}
		}
		sb.append("}");
		lcamGenerateHelper.fillFile(className.toLowerCase()+"\\model",voName,sb);
	}
	
	public boolean isExtend(String fieldName) {
		boolean bool=false;
		Class<PSBaseVO> cla=PSBaseVO.class;
		Field[] fields=cla.getDeclaredFields();
		for(Field field:fields) {
			if(field.getName().equals(fieldName)) {
				bool=true;
				break;
			}
		}
		return bool;
	}
}
